package ucd.wirelessnetworkmonitor;

import java.util.List;

import ucd.wirelessnetworkmonitor.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WiFiScanListAdapter extends BaseAdapter {
	
	Context context;
	List<WiFiScanRowItem> rowItems;
	
	public WiFiScanListAdapter(Context context, List<WiFiScanRowItem> items) {
		this.context = context;
		this.rowItems = items;
	}
	
	private class ViewHolder {
		TextView ssidTextView;
		TextView rssiTextView;
		TextView bssidTextView;
		TextView securityTextView;
	}

	public int getCount() {
		return rowItems.size();
	}

	public Object getItem(int index) {
		return rowItems.get(index);
	}

	public long getItemId(int index) {
		return rowItems.indexOf(getItem(index));
	}

	public View getView(int index, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		
		LayoutInflater inflater = (LayoutInflater)
				context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			
			convertView = inflater.inflate(R.layout.wifi_scan_list_item, null);
			holder = new ViewHolder();
			holder.ssidTextView = (TextView) convertView.findViewById(R.id.list_item_ssid);
			holder.rssiTextView = (TextView) convertView.findViewById(R.id.list_item_rssi);
			holder.bssidTextView = (TextView) convertView.findViewById(R.id.list_item_bssid);
			holder.securityTextView = (TextView) convertView.findViewById(R.id.list_item_security);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		WiFiScanRowItem rowItem = (WiFiScanRowItem) getItem(index);
		holder.ssidTextView.setText("SSID: " + rowItem.getSSID());
		holder.rssiTextView.setText("RSSI: " + rowItem.getRSSI() + " dBm");
		holder.bssidTextView.setText("BSSID: " + rowItem.getBSSID());
		holder.securityTextView.setText("Security: " + rowItem.getSecurity());
		
		return convertView;
	}
}
